package com.hiv.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类
 */
@Slf4j
public class StringUtil {

    private static final String REGEX = "(##)(.*)(##\n)(.*)";
    private static final String CHAPTER_REGEX = "(第?\\s*[一二两三四五六七八九十○零百千万亿0-9１２３４５６７８９０]{1,6}\\s*[章回卷节折篇幕集]\\s*.*?)[_,-]?";
    public static final String USERNAME_REGEX = "^[A-Za-z][-_!@#$%^&*a-zA-Z0-9]{4,15}$";
    public static final String PASSWORD_REGEX = "^(?![a-zA-z]+$)(?!\\d+$)(?![-_!@#$%^&*]+$)[a-zA-Z\\d-_!@#$%^&*]{6,16}$";
    public static final String MOBILE_REGEX = "^1\\d{10}$";
    public static final String EMAIL_REGEX = "^\\w+@\\w+(\\.[a-zA-Z]{2,3}){1,2}$";
    public static final String ID_CARD_REGEX = "^[0-9]{18}$|^[0-9]{15}$";

    public static final Pattern PATTERN = Pattern.compile(REGEX);
    public static final Pattern PATTERN_CHAPTER= Pattern.compile(CHAPTER_REGEX);
    public static final Pattern PATTERN_USERNAME= Pattern.compile(USERNAME_REGEX);
    public static final Pattern PATTERN_PASSWORD= Pattern.compile(PASSWORD_REGEX);
    public static final Pattern PATTERN_MOBILE= Pattern.compile(MOBILE_REGEX);
    public static final Pattern PATTERN_EMAIL= Pattern.compile(EMAIL_REGEX);
    public static final Pattern PATTERN_ID_CARD= Pattern.compile(ID_CARD_REGEX);


    /**
     * 获取章节名称
     * @param title
     * @return
     */
    public static String getChapterName(String title) {
        if (StringUtils.isEmpty(title)) {
            return "";
        }
        Matcher matcher = PATTERN_CHAPTER.matcher(title);
        while (matcher.find()) {
            return matcher.group();
        }
        return "";
    }


    /**
     *
     * @param field
     * @return
     */
    public static String ifNullReturnEpty(String field) {
        if (StringUtils.isEmpty(field)) {
            return "";
        } else {
            return field;
        }
    }


    /**
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }

    /**
     *
     * @param startDateStr
     * @param endDateStr
     * @param middleDateStr
     * @return
     */
    public static boolean isInMiddle(String startDateStr, String endDateStr, String middleDateStr) {
        try{
            Date startDate = DateUtils.parseDateNoTime(startDateStr, DateUtils.WEB_FORMAT);
            Date endDate = DateUtils.parseDateNoTime(endDateStr, DateUtils.WEB_FORMAT);
            Date middleDate = DateUtils.parseDateNoTime(middleDateStr, DateUtils.WEB_FORMAT);
            return  middleDate.getTime() >= startDate.getTime() && middleDate.getTime() <= endDate.getTime();
        }catch (Exception e){
            log.error("isInMiddle err: {}", e.getMessage(), e);
            return false;
        }
    }

    public static String generateVerifyCode(){
        return String.valueOf((int)((Math.random()*9+1)*100000));
    }

    public static String generateSerialNoSurfix(){
        return String.valueOf((int)((Math.random()*9+1)*100000));
    }
}
